<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org">

<head th:replace="component/head :: head"></head>

<body onload="fillHref();">
	<header th:replace="component/header :: header"></header>

	<!-- Begin page content -->
	<div class="container">
		<div class="row">
			<div th:replace="component/sidebar"></div>

			<div class="col-md-9">
				<div th:replace="component/page-title::pageTitle('图书录入页')"></div>

				<div class="row">
					<div class="col-md-8 offset-md-2">
						<div class="input-group mb-3">
							<div class="input-group-prepend">
								<span class="input-group-text" id="basic-addon3">书名</span>
							</div>
							<input type="text" class="form-control" id="name"
								aria-describedby="basic-addon3">
						</div>
						<div class="input-group mb-3">
							<div class="input-group-prepend">
								<span class="input-group-text" id="basic-addon3">书编</span>
							</div>
							<input type="text" class="form-control" id="number"
								aria-describedby="basic-addon3">
						</div>
						<div class="input-group mb-3">
							<div class="input-group-prepend">
								<span class="input-group-text" id="basic-addon3">出版社</span>
							</div>
							<input type="text" class="form-control" id="press"
								aria-describedby="basic-addon3">
						</div>
						<div class="input-group mb-3">
							<div class="input-group-prepend">
								<span class="input-group-text" id="basic-addon3">作者</span>
							</div>
							<input type="text" class="form-control" id="author"
								aria-describedby="basic-addon3">
						</div>
					</div>
				</div>

				<div class="row">
					<div class="col-md-8 offset-md-2">
						<button onclick="insert()" type="button" class="btn btn-primary">录入</button>
						<a href="#" class="btn btn-secondary" role="button"
							id="user-home">返回</a>
					</div>
				</div>
			</div>
		</div>
	</div>

	<footer th:replace="component/footer :: footer"></footer>

	<div th:replace="component/footer-lib"></div>
	<script type="text/javascript">
		function insert() {
			//获取表单数据
			var name = $('#name').val();
			var number = $('#number').val();
			var press = $('#press').val();
			var author = $('#author').val();

			$
					.ajax({
						url : "/restmanage/insert-book",
						type : "POST",
						contentType : "application/json; charset=utf-8",
						data : JSON.stringify({
							"name" : name,
							"number" : number,
							"press" : press,
							"author" : author
						}),
						dataType : 'json',
						async : true,
						success : function(result) {
							if (result.success == false) {
								if (result.code == 9002) {
									alert("参数错误");
								} else if (result.code == 9999) {
									alert("系统错误");
								} else if (result.code == 1007) {
									alert("已存在该书编，若要录入新书，请检查书编！若要增加库存，请检查该书其他信息是否输入正确！");
								}
							} else {
								alert("录入成功");
								location.reload();
							}
						},
						error : function(e) {
							alert("异常");
						}
					});
		}
		
		function fillHref() {
			document.getElementById("user-home").href = "/user/home";
		}
	</script>

</body>

</html>